import os
from cv2.detail import ExposureCompensator_GAIN_BLOCKS
import imageio
import imageio.v3 as iio
from moviepy import video  # 推荐用 v3 接口

def read_video_frames(video_path):
    frames = []
    # 逐帧读取
    for frame in iio.imiter(video_path):
        frames.append(frame)  # frame 是 numpy 数组，形状为 (H, W, 3)
    return frames

path = "background_2"
frames_list = []
min_len = 300
for i in range(6):
    video_path = os.path.join(path, f"{i}.mp4")
    frames = read_video_frames(video_path)
    frames_list.append(frames)
    if len(frames) < min_len:
        min_len = len(frames)
    
for i in range(len(frames_list)):
    frames_list[i] = frames_list[i][:min_len]

for i in range(len(frames_list)):
    save_path = os.path.join(path, f"{i}_align")
    os.makedirs(save_path, exist_ok=True)
    for j, frame in enumerate(frames_list[i]):
        iio.imwrite(os.path.join(save_path,f"{j:04d}.png"), frame)

    